package com.haohe.wx.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;

import java.text.ParseException;

public class DateUtils {
	private static Logger cLogger = Logger.getLogger(DateUtils.class);

	  public static Date parseDate(String pDateString, String pDateFormat) {
	    try {
	      return new SimpleDateFormat(pDateFormat).parse(pDateString);
	    } catch (ParseException ex) {
	      cLogger.error("日期-格式不匹配！" + pDateString + ":" + pDateFormat, ex);
	    }return null;
	  }

	  public static String formatTrans(String pDate, String pOldFormat, String pNewFormat)
	  {
	    if ((null == pDate) || ("".equals(pDate))) {
	      return pDate;
	    }

	    return new SimpleDateFormat(pNewFormat).format(parseDate(pDate, pOldFormat));
	  }

	  public static String date10to8(String pDate)
	  {
	    if ((null == pDate) || ("".equals(pDate))) {
	      return pDate;
	    }

	    return pDate.substring(0, 4) + pDate.substring(5, 7) + pDate.substring(8);
	  }

	  public static String date8to10(String pDate)
	  {
	    if ((null == pDate) || ("".equals(pDate))) {
	      return pDate;
	    }

	    char[] mChars = pDate.toCharArray();

	    return new StringBuilder().append(mChars, 0, 4).append('-').append(mChars, 4, 2).append('-').append(mChars, 6, 2).toString();
	  }

	  public static String time8to6(String pTime)
	  {
	    if ((null == pTime) || ("".equals(pTime))) {
	      return pTime;
	    }
	    for (int i = pTime.length(); i < 8; i++) {
	      pTime = '0' + pTime;
	    }

	    return pTime.substring(0, 2) + pTime.substring(3, 5) + pTime.substring(6);
	  }

	  public static String time6to8(String pTime)
	  {
	    if ((null == pTime) || ("".equals(pTime))) {
	      return pTime;
	    }
	    for (int i = pTime.length(); i < 6; i++) {
	      pTime = '0' + pTime;
	    }

	    char[] mChars = pTime.toCharArray();
	    return new StringBuilder().append(mChars, 0, 2).append(':').append(mChars, 2, 2).append(':').append(mChars, 4, 2).toString();
	  }

	  public static String getCur10Date()
	  {
	    return get10Date(Calendar.getInstance());
	  }

	  public static int getCur8Date()
	  {
	    return get8Date(Calendar.getInstance());
	  }

	  public static String getCur8Time()
	  {
	    return get8Time(Calendar.getInstance());
	  }

	  public static int getCur6Time()
	  {
	    return get6Time(Calendar.getInstance());
	  }

	  public static String getCurDateTime()
	  {
	    Calendar mCurCalendar = Calendar.getInstance();
	    StringBuilder mStrBuilder = new StringBuilder().append(mCurCalendar.get(1)).append('-');
	    int mTempInt = mCurCalendar.get(2);
	    mTempInt++; if (mTempInt < 10) {
	      mStrBuilder.append('0');
	    }
	    mStrBuilder.append(mTempInt).append('-');
	    mTempInt = mCurCalendar.get(5);
	    if (mTempInt < 10) {
	      mStrBuilder.append('0');
	    }
	    mStrBuilder.append(mTempInt).append(' ');
	    mTempInt = mCurCalendar.get(11);
	    if (mTempInt < 10) {
	      mStrBuilder.append('0');
	    }
	    mStrBuilder.append(mTempInt).append(':');
	    mTempInt = mCurCalendar.get(12);
	    if (mTempInt < 10) {
	      mStrBuilder.append('0');
	    }
	    mStrBuilder.append(mTempInt).append(':');
	    mTempInt = mCurCalendar.get(13);
	    if (mTempInt < 10) {
	      mStrBuilder.append('0');
	    }
	    mStrBuilder.append(mTempInt);
	    return mStrBuilder.toString();
	  }

	  public static String getCurDate(String pDateFormat) {
	    return new SimpleDateFormat(pDateFormat).format(new Date());
	  }

	  public static String get10Date(Calendar pCalendar)
	  {
	    StringBuilder mStrBuilder = new StringBuilder().append(pCalendar.get(1)).append('-');
	    int mTempInt = pCalendar.get(2);
	    mTempInt++; if (mTempInt < 10) {
	      mStrBuilder.append('0');
	    }
	    mStrBuilder.append(mTempInt).append('-');
	    mTempInt = pCalendar.get(5);
	    if (mTempInt < 10) {
	      mStrBuilder.append('0');
	    }
	    mStrBuilder.append(mTempInt);
	    return mStrBuilder.toString();
	  }

	  public static String get10Date(long pMillis)
	  {
	    Calendar mCurCalendar = Calendar.getInstance();
	    mCurCalendar.setTimeInMillis(pMillis);
	    return get10Date(mCurCalendar);
	  }

	  public static String get10Date(Date pDate)
	  {
	    Calendar mCurCalendar = Calendar.getInstance();
	    mCurCalendar.setTime(pDate);
	    return get10Date(mCurCalendar);
	  }

	  public static int get8Date(Calendar pCalendar)
	  {
	    return pCalendar.get(1) * 10000 + (pCalendar.get(2) + 1) * 100 + pCalendar.get(5);
	  }

	  public static int get8Date(long pMillis)
	  {
	    Calendar mCurCalendar = Calendar.getInstance();
	    mCurCalendar.setTimeInMillis(pMillis);
	    return get8Date(mCurCalendar);
	  }

	  public static int get8Date(Date pDate)
	  {
	    Calendar mCurCalendar = Calendar.getInstance();
	    mCurCalendar.setTime(pDate);
	    return get8Date(mCurCalendar);
	  }

	  public static String get8Time(Calendar pCalendar)
	  {
	    StringBuilder mStrBuilder = new StringBuilder();
	    int mTempInt = pCalendar.get(11);
	    if (mTempInt < 10) {
	      mStrBuilder.append('0');
	    }
	    mStrBuilder.append(mTempInt).append(':');
	    mTempInt = pCalendar.get(12);
	    if (mTempInt < 10) {
	      mStrBuilder.append('0');
	    }
	    mStrBuilder.append(mTempInt).append(':');
	    mTempInt = pCalendar.get(13);
	    if (mTempInt < 10) {
	      mStrBuilder.append('0');
	    }
	    mStrBuilder.append(mTempInt);
	    return mStrBuilder.toString();
	  }

	  public static String get8Time(long pMillis)
	  {
	    Calendar mCurCalendar = Calendar.getInstance();
	    mCurCalendar.setTimeInMillis(pMillis);
	    return get8Time(mCurCalendar);
	  }

	  public static String get8Time(Date pDate)
	  {
	    Calendar mCurCalendar = Calendar.getInstance();
	    mCurCalendar.setTime(pDate);
	    return get8Time(mCurCalendar);
	  }

	  public static int get6Time(Calendar pCalendar)
	  {
	    return pCalendar.get(11) * 10000 + pCalendar.get(12) * 100 + pCalendar.get(13);
	  }

	  public static int get6Time(long pMillis)
	  {
	    Calendar mCurCalendar = Calendar.getInstance();
	    mCurCalendar.setTimeInMillis(pMillis);
	    return get6Time(mCurCalendar);
	  }

	  public static int get6Time(Date pDate)
	  {
	    Calendar mCurCalendar = Calendar.getInstance();
	    mCurCalendar.setTime(pDate);
	    return get6Time(mCurCalendar);
	  }

	  public static String getDateStr(Calendar pCalendar, String pFormat) {
	    return new SimpleDateFormat(pFormat).format(pCalendar.getTime());
	  }

	  public static String getDateStr(long pMillis, String pFormat) {
	    return new SimpleDateFormat(pFormat).format(new Date(pMillis));
	  }

	  public static String getDateStr(Date pDate, String pFormat) {
	    return new SimpleDateFormat(pFormat).format(pDate);
	  }

	  public static int getAge(Date pBirthday) {
	    Calendar mBirthdayCalendar = Calendar.getInstance();
	    mBirthdayCalendar.setTime(pBirthday);

	    Calendar mNowCalendar = Calendar.getInstance();

	    int mAge = mNowCalendar.get(1) - mBirthdayCalendar.get(1);

	    mBirthdayCalendar.add(1, mAge);
	    if (mNowCalendar.before(mBirthdayCalendar)) {
	      mAge--;
	    }

	    return mAge;
	  }

	  public static int getAge(String p8Birthday)
	  {
	    return getAge(Integer.parseInt(p8Birthday));
	  }

	  public static int getAge(int p8Birthday)
	  {
	    Calendar mNowCalendar = Calendar.getInstance();
	    int mAge = mNowCalendar.get(1) - p8Birthday / 10000;
	    int mBirthMonth = p8Birthday % 10000 / 100;
	    int mCurMonth = mNowCalendar.get(2) + 1;
	    mAge--; return (mCurMonth < mBirthMonth) || ((mBirthMonth == mCurMonth) && (mNowCalendar.get(5) < p8Birthday % 100)) ? mAge : mAge;
	  }

	  public static int compareYear(Date pFirDate, Date pSecDate)
	  {
	    Date mSmlDate = pFirDate;
	    Date mBigDate = pSecDate;
	    if (pSecDate.before(pFirDate)) {
	      mSmlDate = pSecDate;
	      mBigDate = pFirDate;
	    }

	    Calendar mSmlCalendar = Calendar.getInstance();
	    mSmlCalendar.setTime(mSmlDate);

	    Calendar mBigCalendar = Calendar.getInstance();
	    mBigCalendar.setTime(mBigDate);

	    int mDifYear = mBigCalendar.get(1) - mSmlCalendar.get(1);

	    mSmlCalendar.add(1, mDifYear);
	    if (mBigCalendar.before(mSmlCalendar)) {
	      mDifYear--;
	    }

	    return mDifYear;
	  }

	  public static int compareYear(String pFir8Date, String pSec8Date)
	  {
	    return compareYear(Integer.parseInt(pFir8Date), Integer.parseInt(pSec8Date));
	  }

	  public static int compareYear(int pFir8Date, int pSec8Date)
	  {
	    return pFir8Date > pSec8Date ? (pFir8Date - pSec8Date) / 10000 : (pSec8Date - pFir8Date) / 10000;
	  }

	  public static int compareMonth(Date pFirDate, Date pSecDate)
	  {
	    Date mSmlDate = pFirDate;
	    Date mBigDate = pSecDate;
	    if (pSecDate.before(pFirDate)) {
	      mSmlDate = pSecDate;
	      mBigDate = pFirDate;
	    }

	    Calendar mSmlCalendar = Calendar.getInstance();
	    mSmlCalendar.setTime(mSmlDate);

	    Calendar mBigCalendar = Calendar.getInstance();
	    mBigCalendar.setTime(mBigDate);

	    int mDifYear = mBigCalendar.get(1) - mSmlCalendar.get(1);

	    int mDifMonth = mBigCalendar.get(2) - mSmlCalendar.get(2);
	    if (mDifMonth < 0) {
	      mDifYear--;
	      mDifMonth += 12;
	    }

	    mDifMonth += mDifYear * 12;

	    mSmlCalendar.add(2, mDifMonth);
	    if (mBigCalendar.before(mSmlCalendar)) {
	      mDifMonth--;
	    }

	    return mDifMonth;
	  }

	  public static int compareMonth(String pFir8Date, String pSec8Date)
	  {
	    return compareMonth(Integer.parseInt(pFir8Date), Integer.parseInt(pSec8Date));
	  }

	  public static int compareMonth(int pFir8Date, int pSec8Date)
	  {
	    if (pFir8Date < pSec8Date) {
	      int tTemp = pFir8Date;
	      pFir8Date = pSec8Date;
	      pSec8Date = tTemp;
	    }
	    int mDifYear = (pFir8Date - pSec8Date) / 10000;
	    int mDifMonth = pFir8Date % 10000 - pSec8Date % 10000;
	    if (mDifMonth < 0) {
	      mDifMonth += 1200;
	    }
	    return mDifYear * 12 + mDifMonth / 100;
	  }

	  public static int compareDay(Date pFirDate, Date pSecDate)
	  {
	    Date mSmlDate = pFirDate;
	    Date mBigDate = pSecDate;
	    if (pSecDate.before(pFirDate)) {
	      mSmlDate = pSecDate;
	      mBigDate = pFirDate;
	    }

	    return (int)((mBigDate.getTime() - mSmlDate.getTime() + 3600000L) / 86400000L);
	  }

	  public static int compareDay(String pFir8Date, String pSec8Date)
	  {
	    return compareDay(Integer.parseInt(pFir8Date), Integer.parseInt(pSec8Date));
	  }

	  public static int compareDay(int pFir8Date, int pSec8Date)
	  {
	    if (pFir8Date < pSec8Date) {
	      int tTemp = pFir8Date;
	      pFir8Date = pSec8Date;
	      pSec8Date = tTemp;
	    }
	    Calendar mSmlCalendar = Calendar.getInstance();
	    mSmlCalendar.set(pSec8Date / 10000, pSec8Date % 10000 / 100 - 1, pSec8Date % 100);

	    Calendar mBigCalendar = (Calendar)mSmlCalendar.clone();
	    mBigCalendar.set(pFir8Date / 10000, pFir8Date % 10000 / 100 - 1, pFir8Date % 100);

	    return (int)((mBigCalendar.getTimeInMillis() - mSmlCalendar.getTimeInMillis() + 3600000L) / 86400000L);
	  }

	  public static String getLastDayOfMonth(int pYear, int pMonth) {
	    Calendar mFirDayOfNextMonth = new GregorianCalendar(pYear, pMonth, 1);
	    mFirDayOfNextMonth.add(5, -1);
	    return get10Date(mFirDayOfNextMonth);
	  }

	  public static String getNextDay(String appDate)
	  {
	    return getFutureDay(appDate, "yyyy-MM-dd", 1);
	  }

	  public static String getFutureDay(String appDate, int days)
	  {
	    return getFutureDay(appDate, "yyyy-MM-dd", days);
	  }

	  public static String getFutureDay(String appDate, String format, int days)
	  {
	    String future = "";
	    try {
	      Calendar calendar = GregorianCalendar.getInstance();
	      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
	      Date date = simpleDateFormat.parse(appDate);
	      calendar.setTime(date);
	      calendar.add(5, days);
	      date = calendar.getTime();
	      future = simpleDateFormat.format(date);
	    }
	    catch (Exception e)
	    {
	    }
	    return future;
	  }
}
